package com.rds.web.controller.pms;

import com.rds.common.core.controller.BaseController;
import com.rds.common.core.domain.AjaxResult;
import com.rds.common.core.service.IDynamicService;
import com.rds.common.utils.rds.LifecycleUtil;
import com.rds.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 流程相关处理类
 * @version : 1.0
 * @Copyright: Copyright(c) 2020-2021 All right reserved
 * @Company: GUANGZHOU RDS Co.,LTD
 * @author: LiChuanlong
 * @create Date : 2021/12/8 : 16:34
 */
@RestController
@RequestMapping("/pms/lifecycle")
public class LifeCycleController extends BaseController {
    @Autowired
    private IDynamicService dynamicService;
    @Autowired
    private IRdaTypeService typeService;
    @Autowired
    private IRdaPolicyService policyService;

    /**
     * xxx-详情页-生命周期管理-生命周期提升
     * @author LiChuanlong
     * @date  2021/12/8 : 16:43
     * @param id 对象Id
     * @param policyid 对象策略Id
     * @return 提升成功或失败标识
     */
    @GetMapping("/promote")
    public AjaxResult promote(Long id, Long policyid) {
        System.out.println("LifeCycleController:promote--->>>id:" + id);
        String typeIds = policyService.selectRdaPolicyById(policyid).getTypeIds();
        //System.out.println("LifeCycleController:promote--->>>typeIds:" + typeIds);
        //如果当前通过策略查询出来的多个类型Id 那么只需要取其中一个即可,因为共用policy 管控的模块一定会一致
        String typeId = typeIds.split(",")[0];
        String moduleName = typeService.selectRdaTypeById(Long.parseLong(typeId)).getModuleName();
        System.out.println("LifeCycleController:promote--->>>moduleName:" + moduleName);

        if ("document".equalsIgnoreCase(moduleName)) {
            LifecycleUtil lifecycleUtil = new LifecycleUtil(dynamicService);
            return lifecycleUtil.goToTargetState(id,policyid,"promote","document",false);
        } else {
            return error("promote 对象类型不存在,请联系管理员!");
        }
    }
    /**
     * xxx-详情页-生命周期管理-生命周期降级
     */
    @GetMapping("/demotion")
    public AjaxResult demotion(Long id, Long policyid){
        System.out.println("LifeCycleController:demotion--->>>id:" + id);
        String typeIds = policyService.selectRdaPolicyById(policyid).getTypeIds();
        //System.out.println("LifeCycleController:demotion--->>>typeIds:" + typeIds);
        //如果当前通过策略查询出来的多个类型Id 那么只需要取其中一个即可,因为共用policy 管控的模块一定会一致
        String typeId = typeIds.split(",")[0];
        String moduleName = typeService.selectRdaTypeById(Long.parseLong(typeId)).getModuleName();
        System.out.println("LifeCycleController:demotion--->>>moduleName:" + moduleName);

        if ("document".equalsIgnoreCase(moduleName)) {
            LifecycleUtil lifecycleUtil = new LifecycleUtil(dynamicService);
            return lifecycleUtil.goToTargetState(id,policyid,"demote","document",false);
        } else {
            return success("demote 对象类型不存在,请联系管理员!");
        }
    }
}
